<?php

class QLang
{    
    public $code = 'en';
    private $isLoaded = false;

    private $QXC;

    function __construct($qxc)
    {
        $this->QXC = $qxc;
    }

    public function GetLang()
    {
        return $this->code;
    }

    public function SetLang($code)
    {
        $this->code = strtolower($code);

        $this->isLoaded = false;
    }

    public function Get($name)
    {
        $name = trim($name);

        if(!$this->isLoaded)
        {
            $this->loadFile();
        }

        return $this->{$name} ? $this->{$name} : strtoupper($name);
    }

    private function loadFile()
    {
        // Load main translation file first
        $paths = (array)$this->QXC->Config->languages;
        $file = "{$this->code}." . CORE_XML_EXT;

        if ($path = $this->QXC->pathExist($paths, $file))
        {
            $this->QXC->Xml->Open($path)->ToObject(null, false, &$this);
        }

        // Load additional language files if called from module
        if($modulePath = $this->QXC->getGlobal("ModulePath", "QXC"))
        {
            if ($path = $this->QXC->pathExist("{$modulePath}/languages", $file))
            {
                $this->QXC->Xml->Open($path)->ToObject(null, false, &$this);
            }
        }

        return true;
    }
}